package com.read.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class DBUtils {
	private static final Logger log = LoggerFactory.getLogger(DBUtils.class);
	
	private static String dbDriver="com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://localhost:3306/VMS?characterEncoding=utf-8";
	private static String username="root";
	private static String passwd="java";
	
	private static Connection con ;
	
	public static Connection getCon(){
		try {
			Class.forName(dbDriver);
		} catch (ClassNotFoundException e) {
			log.error("加载数据库驱动类失败，{}不存在",dbDriver);
			e.printStackTrace();
		}
		try {
			con = DriverManager.getConnection(url, username, passwd);
			if(con != null){
				log.info("数据库连接成功");
			}else{
				log.error("数据库连接失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	public static void dbClear(ResultSet rs,Statement st,Connection con){
		try {
			if(rs != null){
				rs.close();
			}
			if(st != null){
				st.close();
			}
			if(con != null){
				con.close();
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		DBUtils.getCon();
	}
}
